local assets =
{
    Asset("ANIM", "anim/tf_crab.zip"),
}

local tf_crab_prefabs =
{
    "tf_dead_crab",
    "tf_dead_crab_cooked",
}

local tf_dead_crab_prefabs =
{
    "tf_dead_crab_cooked",
}

local function fn(bank, build, char_anim_build, data)
    local inst = CreateEntity()

    data = data or {}

    inst.entity:AddTransform()
    inst.entity:AddAnimState()
    inst.entity:AddNetwork()

    MakeInventoryPhysics(inst)

    inst.AnimState:SetBank(bank)
    inst.AnimState:SetBuild(build)
    inst.AnimState:PlayAnimation("idle", false)

    inst:AddTag("fish")
    inst:AddTag("pondfish")
    inst:AddTag("meat")
    inst:AddTag("catfood")
    inst:AddTag("smallcreature")

    if data.weight_min ~= nil and data.weight_max ~= nil then
        inst:AddTag("weighable_fish")
    end

    inst.entity:SetPristine()

    if not TheWorld.ismastersim then
        return inst
    end

    inst.build = char_anim_build --This is used within SGwilson, sent from an event in fishingrod.lua

    inst:AddComponent("bait")

    inst:AddComponent("perishable")
    inst.components.perishable:SetPerishTime(data.perish_time)
    inst.components.perishable:StartPerishing()
    inst.components.perishable.onperishreplacement = data.perish_product
    inst.components.perishable.ignorewentness = true

    inst:AddComponent("cookable")
    inst.components.cookable.product = data.cookable_product

    inst:AddComponent("inspectable")

    inst:AddComponent("inventoryitem")
    inst.components.inventoryitem:SetSinks(true)

    inst:AddComponent("murderable")

    inst:AddComponent("lootdropper")
    inst.components.lootdropper:SetLoot(data.loot)

    inst:AddComponent("edible")
    inst.components.edible.ismeat = true
    inst.components.edible.healthvalue = data.healthvalue
    inst.components.edible.hungervalue = data.hungervalue
    inst.components.edible.sanityvalue = 0
    inst.components.edible.foodtype = FOODTYPE.MEAT

    MakeHauntableLaunchAndPerish(inst)

    inst:AddComponent("tradable")
    inst.components.tradable.goldvalue = data.goldvalue
    inst.data = {}

    return inst
end

local function commonfn(build, anim, loop, cookable)
    local inst = CreateEntity()

    inst.entity:AddTransform()
    inst.entity:AddAnimState()
    inst.entity:AddNetwork()

    MakeInventoryPhysics(inst)

    inst.AnimState:SetBank("tf_crab")
    inst.AnimState:SetBuild("tf_crab")
    inst.AnimState:PlayAnimation(anim, loop)

    inst:AddTag("meat")
    inst:AddTag("catfood")
    inst:AddTag("fishmeat")

    if cookable then
        inst:AddTag("cookable")
    end

    inst.entity:SetPristine()

    if not TheWorld.ismastersim then
        return inst
    end

    inst.build = build --This is used within SGwilson, sent from an event in fishingrod.lua

    inst:AddComponent("edible")
    inst.components.edible.ismeat = true
    inst.components.edible.foodtype = FOODTYPE.MEAT

    inst:AddComponent("stackable")
    inst.components.stackable.maxsize = TUNING.STACK_SIZE_SMALLITEM

    inst:AddComponent("bait")

    inst:AddComponent("perishable")
    inst.components.perishable:SetPerishTime(TUNING.PERISH_FAST)
    inst.components.perishable:StartPerishing()

    if cookable then
        inst:AddComponent("cookable")
        inst.components.cookable.product = "tf_dead_crab_cooked"
    end

    MakeHauntableLaunchAndPerish(inst)

    inst:AddComponent("inspectable")

    inst:AddComponent("inventoryitem")

    MakeHauntableLaunchAndPerish(inst)

    inst:AddComponent("tradable")
    inst.components.tradable.goldvalue = TUNING.GOLD_VALUES.RAREMEAT
    inst.data = {}

    return inst
end

local tf_crab_data =
{
    perish_product = "tf_dead_crab",
    loot = { "tf_dead_crab" },
    cookable_product = "tf_dead_crab_cooked",
    healthvalue = 1,
    hungervalue = 33,
    perish_time = TUNING.PERISH_SUPERFAST,
    goldvalue = TUNING.GOLD_VALUES.RAREMEAT,
}
local function alivefn()
    return fn("tf_crab", "tf_crab", "tf_crab", tf_crab_data)
end

local function rawfn()
    local inst = commonfn("tf_crab", "dead", false, true)
    MakeInventoryFloatable(inst, "med", 0.05, { 1.25, 0.6, 1.25 })

    if not TheWorld.ismastersim then
        return inst
    end

    inst.components.edible.healthvalue = 1
    inst.components.edible.hungervalue = 33
    inst.components.edible.sanityvalue = -20
    inst.components.perishable:SetPerishTime(TUNING.PERISH_SUPERFAST)

    return inst
end

local function cookedfn()
    local inst = commonfn("tf_crab", "cooked")

    MakeInventoryFloatable(inst, "small", 0.1, 0.82)

    if not TheWorld.ismastersim then
        return inst
    end

    inst.components.edible.healthvalue = 5
    inst.components.edible.hungervalue = 35
    inst.components.perishable:SetPerishTime(TUNING.PERISH_FAST)

    return inst
end


return Prefab("tf_crab", alivefn, assets, tf_crab_prefabs),
    Prefab("tf_dead_crab", rawfn, assets, tf_dead_crab_prefabs),
    Prefab("tf_dead_crab_cooked", cookedfn, assets)
